package com.clz.mycustomview

import android.content.Context
import android.graphics.PixelFormat
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.widget.ImageView

import kotlinx.android.synthetic.main.activity_window_manger.*


class WindowMangerActivity : AppCompatActivity(), View.OnClickListener, View.OnTouchListener {
    companion object {
        const val TAG: String = "WindowMangerActivity"
    }
    private var mWindowManger: WindowManager? = null
    private var mImageView: ImageView? = null
    private var mLayoutParams: WindowManager.LayoutParams? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_window_manger)
        btnAdd.setOnClickListener(this)
        btnRemove.setOnClickListener(this)
        mWindowManger = applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager?
    }

    /**
     * Window的类型
     * 应用Window，对应着Activity
     * 子Window 不能单独存在，需要附属在特定的父window中，比如Dialog就是一个子Window
     * 系统Window 需要声明全号线才能创建，比如toast和系统状态栏都是系统Window
     * Window 是分级的，层级大的Window会覆盖在层级小的Window上
     * 应用Window的层级范围 1~99
     * 子Window的层级范围 1000~1999
     * 系统Window的层级范围 2000~2999
     */
    override fun onClick(v: View?) {
        when(v?.id){
            R.id.btnAdd -> {
                mImageView = ImageView(this)
                mImageView?.setBackgroundResource(R.mipmap.ic_launcher_round)
                mLayoutParams = WindowManager.LayoutParams(
                        WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, 2099,
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                                or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                                or WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        ,
                        PixelFormat.TRANSPARENT)
                mLayoutParams?.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
                mLayoutParams?.gravity = Gravity.TOP or Gravity.START
                mLayoutParams?.x = 0
                mLayoutParams?.y = 300
                mImageView?.setOnTouchListener(this)
                mWindowManger?.addView(mImageView,mLayoutParams)
            }
            R.id.btnRemove -> {
                mWindowManger?.removeViewImmediate(mImageView)
            }
        }
    }

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        val rawX : Int? = event?.rawX?.toInt()
        val rawY : Int? = event?.rawY?.toInt()
        when(event?.action){
            MotionEvent.ACTION_MOVE -> {
                mLayoutParams?.x = rawX
                mLayoutParams?.y = rawY
                mWindowManger?.updateViewLayout(mImageView,mLayoutParams)
            }
        }
        return false
    }
}